<?php
// +----------------------------------------------------------------------
// | Description: 客户
// +----------------------------------------------------------------------
// | Author: Michael_xu | gengxiaoxu@5kcrm.com 
// +----------------------------------------------------------------------

namespace app\crm\controller;

use app\admin\controller\ApiCommon;
use app\crm\model\ConfigData;
use think\Hook;
use think\Request;
use think\Db;
use PHPExcel_IOFactory;
use PHPExcel_Cell;
use PHPExcel;

//开票
class Invoicing extends ApiCommon
{
    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @other 其他根据系统设置
    **/    
    public function _initialize()
    {
        $action = [
            'permission'=>['exceldownload','setfollow'],
            'allow'=>['']            
        ];
        Hook::listen('check_auth',$action);
        $request = Request::instance();
        $a = strtolower($request->action());        
        if (!in_array($a, $action['permission'])) {
            parent::_initialize();
        } else {
            $param = Request::instance()->param();          
            $this->param = $param;            
        }
    } 

    /**
     * 开票列表
     * @author Michael_xu
     * @return
     */
    public function index()
    {
        $invoicingModel = model('Invoicing');
        $param = $this->param;
        $userInfo = $this->userInfo;
        $param['user_id'] = $userInfo['id']; 
        $data = $invoicingModel->getDataList($param);       
        return resultArray(['data' => $data]);
    }


    /**
     * 申请开票
     * @author Michael_xu
     * @param 
     * @return
     */
    public function save()
    {
        $invoicingModel = model('Invoicing');
        $param = $this->param;
        $userInfo = $this->userInfo;
        $param['create_user_id'] = $userInfo['id'];
        $param['contract_id'] = $param['contract_id'];
        if ($res = $invoicingModel->createData($param)) {
            return resultArray(['data' => $res]);
        } else {
            return resultArray(['error' => $invoicingModel->getError()]);
        }
    }

    /**
     * 开票详情
     * @author Michael_xu
     * @param  
     * @return
     */
    public function read()
    {
        $invoicingModel = model('Invoicing');
        $param = $this->param;
        $userInfo = $this->userInfo;
        $data = $invoicingModel->getDataById($param['invoicing_id']);
        /*if (!$data) {
            return resultArray(['error' => $invoicingModel->getError()]);
        }
        //数据权限判断
        $userModel = new \app\admin\model\User();
        $auth_user_ids = $userModel->getUserByPer('crm', 'invoicing', 'read');
        //读权限
        $roPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'read');
        $rwPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'update');
        //判断是否客户池数据
        $wherePool = $invoicingModel->getWhereByPool();
        $resPool = db('crm_invoicing')->alias('invoicing')->where(['invoicing_id' => $param['id']])->where($wherePool)->find();
        if (!$resPool && !in_array($data['owner_user_id'],$auth_user_ids) && !$roPre && !$rwPre) {
            header('Content-Type:application/json; charset=utf-8');
            exit(json_encode(['code'=>102,'error'=>'无权操作']));
        }*/
        return resultArray(['data' => $data]);
    }

    /**
     * 编辑开票
     * @author Michael_xu
     * @param 
     * @return
     */
    public function update()
    {
        $invoicingModel = model('Invoicing');
        $param = $this->param;
        $userInfo = $this->userInfo;
        //数据详情
        $data = $invoicingModel->getDataById($param['id']);
        if (!$data) {
            return resultArray(['error' => $invoicingModel->getError()]);
        }
        //数据权限判断
        $userModel = new \app\admin\model\User();
        $auth_user_ids = $userModel->getUserByPer('crm', 'invoicing', 'update');
        //读写权限
        $rwPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'update');     
        //判断是否客户池数据
        $wherePool = $invoicingModel->getWhereByPool();
        $resPool = db('crm_invoicing')->alias('invoicing')->where(['invoicing_id' => $param['id']])->where($wherePool)->find();
        if (!$resPool && !in_array($data['owner_user_id'],$auth_user_ids) && !$rwPre) {
            header('Content-Type:application/json; charset=utf-8');
            exit(json_encode(['code'=>102,'error'=>'无权操作']));
        }        

        $param['user_id'] = $userInfo['id'];
        if ($invoicingModel->updateDataById($param, $param['id'])) {
            return resultArray(['data' => '编辑成功']);
        } else {
            return resultArray(['error' => $invoicingModel->getError()]);
        }       
    }

    /**
     * 删除开票
     * @author Michael_xu
     * @param 
     * @return
     */
    public function delete()
    {
        $invoicingModel = model('Invoicing');
        $param = $this->param;
        if (!is_array($param['id'])) {
            $invoicing_id[] = $param['id'];
        } else {
            $invoicing_id = $param['id'];
        }
        $delIds = [];
        $errorMessage = [];

        if ($delIds) {
            $data = $invoicingModel->delDatas($delIds);
            if (!$data) {
                return resultArray(['error' => $invoicingModel->getError()]);
            }
            actionLog($delIds,'','','');                    
        }
        if ($errorMessage) {
            return resultArray(['error' => $errorMessage]);
        } else {
            return resultArray(['data' => '删除成功']);
        }
    }

}